﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SlamCms.Common;

namespace SlamCms.Areas._default
{
	internal static class HtmlHelperExtensions
	{
		public static IHtmlString StaticScript(this HtmlHelper htmlHelper, string fileName)
		{
			var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
			return "<script type=\"text/javascript\" src=\"{0}\"></script>".F(urlHelper.Static(fileName)).ToHtml();
		}

		public static IHtmlString StaticStyleSheet(this HtmlHelper htmlHelper, string fileName)
		{
			var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
			return "<link rel=\"stylesheet\" href=\"{0}\" type=\"text/css\" />".F(urlHelper.Static(fileName)).ToHtml();
		}

		public static IHtmlString StaticImage(this HtmlHelper htmlHelper, string fileName, string alt, object htmlAttributes = null)
		{
			var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
			var imgTag = new TagBuilder("img");
			imgTag.MergeAttribute("src", urlHelper.Static(fileName));
			imgTag.MergeAttribute("alt", alt);
			imgTag.MergeAttribute("title", alt);
			if (htmlAttributes != null)
				imgTag.MergeAttributes(new RouteValueDictionary(htmlAttributes));

			return imgTag.ToString().ToHtml();
		}

		public static IHtmlString Timago(this DateTime? date)
		{
			return date.HasValue ? date.Value.Timago() : MvcHtmlString.Empty;
		}

		public static IHtmlString Timago(this DateTime date)
		{
			var tag = new TagBuilder("abbr");
			tag.AddCssClass("timeago");
			tag.Attributes.Add("title", date.ToUniversalTime().ToString("s") + "Z");
			tag.SetInnerText(date.ToString());
			return MvcHtmlString.Create(tag.ToString());
		}
	}
}